package com.hfb.mashibing.alip8.designpattern._014visitor;

public class Computer {
    ComputerPart cpu = new CPU();
    ComputerPart memory = new Memory(); // 内存
    ComputerPart board = new Board();   // 主板

    public void accept(Visitor v){
        this.cpu.accept(v);
        this.memory.accept(v);
        this.board.accept(v);
    }

    public static void main(String[] args) {
        PersonVisitor v = new PersonVisitor();
        new Computer().accept(v);
        System.out.printf("PersonVisitor:%s\n", v.totalPrice);

        CorpVisitor  cv= new CorpVisitor();
        new Computer().accept(cv);
        System.out.printf("CorpVisitor:%s\n", cv.totalPrice);
    }



    /**
     * 电脑组件
     */
    abstract class ComputerPart {
        abstract void accept(Visitor v);
        // some other operations eq:qetName qetBrand
        abstract double getPrice();

    }
    interface Visitor {
        void visitCpu(CPU cpu);
        void visitMemory(Memory memory);
        void visitBoard(Board board);

    }
    static class PersonVisitor implements Visitor{
        double totalPrice = 0.0;
        @Override
        public void visitCpu(CPU cpu) {
            totalPrice += cpu.getPrice() * 0.9;
        }

        @Override
        public void visitMemory(Memory memory) {
            totalPrice += memory.getPrice() * 0.85;
        }

        @Override
        public void visitBoard(Board board) {
            totalPrice += board.getPrice() * 0.95;
        }
    }
    static class CorpVisitor implements Visitor{
        double totalPrice = 0.0;
        @Override
        public void visitCpu(CPU cpu) {
            totalPrice += cpu.getPrice() * 0.6;
        }

        @Override
        public void visitMemory(Memory memory) {
            totalPrice += memory.getPrice() * 0.65;
        }

        @Override
        public void visitBoard(Board board) {
            totalPrice += board.getPrice() * 0.75;
        }
    }
    class CPU extends ComputerPart {
        @Override
        void accept(Visitor v) {v.visitCpu(this);}

        @Override
        double getPrice() {
            return 1000;
        }
    }
    class Memory extends ComputerPart {

        @Override
        void accept(Visitor v) {
            v.visitMemory(this);
        }

        @Override
        double getPrice() {
            return 300;
        }
    }
    class Board extends ComputerPart {

        @Override
        void accept(Visitor v) {
            v.visitBoard(this);
        }

        @Override
        double getPrice() {
            return 200;
        }
    }
}
